import time

class UtoTimer(object):
    """
    An independent timer class (because separation of application
    and protocol logic is a good thing).
    """
    triggers = ["!time"]

    agestart = 1233360002 - 3600 + (7*13+2)*24*3600     # Daylight saving time
    agestart = 1241391602 + (7*13+4)*24*3600
    name = "time"
    help = ["Utopia WOL timer",
            "%c6!time"%(03)]
    
    hour = 3600
    day = 24*hour
    week = 7*day
    
    weeknames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
    daynames = ['1st','2nd','3rd','4th','5th','6th', '7th','8th',
                '9th','10th','11th','12th','13th','14th','15th','16th',
                '17th','18th','19th','20th','21st','22nd','23rd','24th']
    
    def command(self, firstword, user, channel, msg):
        if msg == "!time":
            now = int(time.time())
            agetime = now-self.agestart
            weeks = agetime/self.week
            agetime = agetime-weeks*self.week
            days = agetime/self.day
            agetime = agetime-days*self.day
            hours = agetime/self.hour
            agetime = agetime-hours*self.hour
            tickleft = 3600-agetime
            mins = tickleft/60
            secs = tickleft-60*mins
            
            return {'chanmsg': ["Current WOL time: %s %s, YR%d."%(self.weeknames[days], self.daynames[hours], weeks),
                                "Time to next tick: %d minutes and %d seconds."%(mins,secs)]}